<?php

namespace App\Http\Controllers\Api;

use App\Common\Comiter;
use App\Dmeo\ShopTypeDao;
use App\Http\Controllers\Controller;
use App\Models\ShopType;
use App\ServerLi\BaiDuServer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ShopTtpeController extends Controller
{
    //
    /**
     * Name:分页展示
     * User: yangrui
     * Date: 2023/6/17
     * Time:8:17
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function ShopTypeList(Request $request){
        if ($request->method() != 'GET') throw  new  \Exception('请求方式不正确');
        $search=$request->input('search');
        $num=$request->input('num');
        $data= ShopTypeDao::ShopTypeList($num,$search);
        if($data){
            return Comiter::success('200','展示成功',$data);
        }else{
            return Comiter::fail('403','展示失败',null);
        }
    }

    /**
     * Name:删除
     * User: yangrui
     * Date: 2023/6/18
     * Time:15:08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function ShopTypeDel(Request $request){
        if ($request->method() != 'POST') throw  new  \Exception('请求方式不正确');
        $status=$request->input('status');
        $id=$request->input('id');
        $pid=$request->input('pid');
        $data=ShopTypeDao::ShopTypeDel($id,$status,$pid);
        if($data){
            return Comiter::success('200','删除成功',null);
        }else{
            return Comiter::fail('403','删除失败');
        }
    }
    /**
     * Name:添加
     * User: yangrui
     * Date: 2023/6/17
     * Time:8:26
     */
    public function ShopTypeCreate(Request $request){
//        $data=$request->validate([
//            'shop_type_name' =>'required',
//            'status' =>'required',
//        ]);
        $fileTmp = $request->file('image');
        $result = BaiDuServer::image($fileTmp);
        if($result['conclusion'] != "合规") return Comiter::success('200','图片合法');
        //云存储
        $disk = Storage::disk('qiniu'); //使用七牛云上传
        $time = date('Y/m/d/H-m-s');
        $file     = $request->file('image');
        $filePath = $file->getRealPath();
        $ext = $file->getClientOriginalExtension();
        $disk->put($time.'.'.$ext, file_get_contents($filePath));//上传
        //图片地址
        $url = 'http://liyan.shop'.$time.'.'.mt_rand(1000,9999).$ext;
//        if(!$filename) return Comiter::fail('400','添加失败');
//        return Comiter::success(200,"添加成功",$url,);
        $data['shop_type_pictyre']=$url;
        $res=ShopTypeDao::ShopTypeCreate($data);
        if(!$res) return Comiter::fail('400','添加失败');
        return Comiter::success(200,"添加成功",$url,);
    }
    //分级展示
public function add(){
    $omit = ShopType::where('pid', 0)->get();
    return view('admin.createshoptype', compact('omit'));
}
//二级分类
    public function Sencond(Request $request){
        $id=$request->input('pid');
        $data=ShopType::where('pid',$id)->get();
        return $data;
    }
    //三级分类
    public function Third(Request $request){
        $id=$request->input('pid');
        $data=ShopType::where('pid',$id)->get();
        return $data;
    }
}
